package com.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailUtils {


    @Autowired
    JavaMailSenderImpl mailSender;


    /**
     * @param html
     * @param subject
     * @param text
     * @return
     * @throws MessagingException
     * @Author sjy
     */

    public   void sendMain(Boolean html, String subject, String text, String mailTo,String photoFile) throws MessagingException {
        System.out.println(mailSender);
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        String[] photoFileArr = photoFile.split(",");
        helper.setSubject(subject);
        helper.setText(text, true);
        //附件
        for (int i = 0; i <photoFileArr.length ; i++) {

            helper.addAttachment("商品"+(i+1)+".jpg", new File("D:\\WorkSpace\\GitWorkSpace\\shopMall\\src\\main\\webapp\\files\\"+photoFileArr[i]));
        }

        helper.setFrom("995251681@qq.com");
        helper.setTo(mailTo);
        mailSender.send(mimeMessage);

    }

}
